package com.demo.project_template.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.demo.project_template.entity.Product;
import com.demo.project_template.entity.ProductSpec;
import com.demo.project_template.exception.ErrorCodeException;
import com.demo.project_template.mapper.ProductMapper;
import com.demo.project_template.mapper.ProductSpecMapper;
import com.demo.project_template.service.ProductSpecService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.project_template.util.CheckEmptyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 规格表 服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2023-06-19 15:56:44
 */
@Slf4j
@Service
public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, ProductSpec> implements ProductSpecService {
    @Resource
    private ProductSpecMapper productSpecMapper;
    @Resource
    private ProductMapper productMapper;

    @Override
    public void insertProductSpec(ProductSpec productSpec) {
        productSpec.setProductSpec(JSON.toJSONString(JSON.parseObject(productSpec.getProductSpec(),TreeMap.class)));
        productSpecMapper.insert(productSpec);
        setProductAttributeList(productSpec.getProductId());
    }

    @Override
    public void updateProductSpec(ProductSpec productSpec) {
        productSpec.setProductSpec(JSON.toJSONString(JSON.parseObject(productSpec.getProductSpec(),TreeMap.class)));
        productSpecMapper.updateById(productSpec);
        setProductAttributeList(productSpec.getProductId());
    }

    @Override
    public void deleteProductSpec(Integer productSpecId,Integer productId) {
        productSpecMapper.deleteById(productSpecId);
        setProductAttributeList(productId);
    }

    @Override
    public ProductSpec selectProductSpec(Integer productId,String productSpec) {
        String sortedProductSpec = JSON.toJSONString(JSON.parseObject(productSpec, TreeMap.class));
        ProductSpec result = productSpecMapper.selectOne(new LambdaUpdateWrapper<ProductSpec>().eq(ProductSpec::getProductId, productId).eq(ProductSpec::getServiceSpecs, sortedProductSpec));
        if (CheckEmptyUtil.isEmpty(result)){
            throw new ErrorCodeException(500,"未找到相关规格");
        }
        return result;
    }

    private void setProductAttributeList(Integer productId){
        List<ProductSpec> productSpecs = productSpecMapper.selectList(new LambdaUpdateWrapper<ProductSpec>().eq(ProductSpec::getProductId, productId));
        LinkedHashMap<String,Set<String>> productSpecsMap = new LinkedHashMap<>();
        LinkedHashMap<String,String> initSpecsMap = JSON.parseObject(productSpecs.get(0).getProductSpec(), LinkedHashMap.class);
        initSpecsMap.forEach((key,value)->{
            productSpecsMap.put(key,new HashSet<>(Collections.singleton(value)));
        });
        for (ProductSpec productSpec : productSpecs) {
            LinkedHashMap<String,String> oneProductSpecs = JSON.parseObject(productSpec.getProductSpec(), LinkedHashMap.class);
            oneProductSpecs.forEach((key,value)->{
                productSpecsMap.get(key).add(value);
            });
        }
        Product product = productMapper.selectById(productId);
        product.setProductAttributeList(JSON.toJSONString(productSpecsMap));
        productMapper.updateById(product);
    }
}
